<!-- 选择单据（单选/多选）功能组件 -->
<template>
  <div class="listBody" style="width: calc(100% - 20px); padding: 0 10px; overflow: hidden;">
    <div class="title" style="margin-bottom: 10px;">
      <div class="name">{{menuName}} >></div>
      <div class="butClose" @click="closeWin"></div>
    </div>
    <!-- 搜索过滤 -->
    <div class="search">
      <div class="floatR">
        <button class="butOne" @click="findData"><img src="../assets/img/find.png" />查询</button>
      </div>
      <div class="floatR">
        <el-input type="text" v-model="findTxt" placeholder="请输入关键字查询"></el-input>
      </div>
    </div>
    <!-- 列表数据 -->
    <div class="table" style="max-height: 350px; overflow-y: auto; overflow-x: hidden;"
      v-if="tableParam != null && tableParam.length > 0">
      <el-table 
      border
      use-virtual
      highlight-current-row
      :data="dataItems"
      ref="multipleTable"
      class="tableList"
      rowKey="id"
      selectable="true"
      @select="handleSelect" 
      @selection-change="handleSelectionChange" 
      :header-cell-style="{ background:'#FFF' }"
      >
        <el-table-column type="selection" width="60" fixed></el-table-column>
        <el-table-column 
          v-for="item in tableParam" 
          style="text-align: center;" 
          :prop="item.paramName" 
          :label="item.name" min-width="60" 
          :width="item.width" 
          v-show="item.hidden" 
          :fixed="item.freeze == 1 ? true:false" 
          sortable>
          <template slot-scope="scope">
            <div v-if="item.type == 'image'" style="width: 100%; text-align: center;">
              <img style="height: 60px; width: auto;"
              :src="scope.row.sex != null && scope.row.sex == 2 ?
              require('../assets/img/girl.png') : require('../assets/img/boy.png')" />
            </div>
             <span v-else-if="scope.row[scope.column.property] != null && scope.row[scope.column.property].toString().length > 0">{{scope.row[scope.column.property]}}</span>
             <span v-else style="font-weight: bold; font-size: 18px;">-</span>
          </template>
        </el-table-column>
        <template slot="empty">
          <div class="notData">
            <img src="../assets/img/notData.png" />暂无数据
          </div>
        </template>
      </el-table>      
    </div>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination
        layout="total, prev, pager, next, sizes"        
        :page-size="pageParam.pageSize"
        :current-page="pageParam.pageIndex"
        :total="pageParam.pageCount"
        :page-sizes="[10,20,50,100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 操作 -->
    <div class="button">
      <button class="butTwo" @click="closeWin">取 消</button>
      <button class="butOne" @click="returnSelectData">确 认</button>
    </div>
  </div>
</template>
<script>

export default {
  data() {
    return {
      menuName: "",
      menuKey: "",
      searchType:'',
      selectType: '',
      dataType:'',
      findSearch: '',
      findType: '',
      filter: '',
      findTxt: '',
      submitUrl: '',
      selectedIds:'',
      tableParam:[],
      dataItems:[],
      pageParam:{
        pageIndex:1,
        pageSize:20,
        pageNum:0,
        pageCount:0
      },
      returnUrl:'',
      returnType: '',
      orgId: 0
    };
  },
  props:{
    theData:{
      type:Object,
      required:true,
      default:{}
    },
    theOrg:{
      type:Number,
      required:true
    }
  },
  created(){
    this.showData();
  },
  watch: {
    theData(data){
      this.menuKey = this.theData.key;
      this.menuName = this.theData.name;
      this.orgId = this.theOrg;
      this.searchType = this.theData.searchType;
      this.returnType = this.theData.returnType;
      this.submitUrl = '';
      this.findTxt = '';
      this.filter = '';
      this.findSearch = '',
      this.selectedIds = '';
      this.tableParam = [];
      this.dataItems = [];
      this.pageParam = {
        pageIndex:1,
        pageSize:20,
        pageNum:0,
        pageCount:0
      };
      console.log('returnType: ' + this.returnType);
      this.showData();
    }
  },
  methods: {
    //展示组件数据
    async showData(){
      if(this.theData != null && this.theData.url != null){        
        let toUrl = this.changeUrl(this.theData.url);
        console.log('find select url:>>' + toUrl);
        let { data: res } = await this.$http.get(toUrl);
        if (res.result) {
          console.log('get select data:>>');
          console.log(res.obj);
          let isCommon = !(this.searchType != null && this.searchType === 'detailSelect');
          if(isCommon){            
            this.dataType = res.obj.dataType;
            this.findType = res.obj.findType;
          }
          this.selectType = res.obj.selectType;
          console.log('selectType: ' + this.selectType);
          this.filter = res.obj.filter;
          this.findSearch = res.obj.findSearch;
          this.tableParam = res.obj.tableParam;
          this.submitUrl = res.obj.submitUrl;
          this.returnUrl = res.returnUrl;
          if(res.returnType != null && res.returnType.length > 0)
            this.returnType = res.returnType;          
          this.pageParam = res.obj.pagingParam;
          let index = this.pageParam.pageIndex != null ? this.pageParam.pageIndex : 1;
          let size = this.pageParam.pageSize != null ? this.pageParam.pageSize : 20;
          this.findData(index,size);
        }else{
          this.clewMsg(res.type,res.msg);
        }
      }
    },
    //搜索列表数据
    async findData(index,size){
      let theSize = size != null && size > 0 ? size : 20;
      let theIndex = index != null && index > 0 ? index : 1;
      let searchData = '{ pageIndex: '+theIndex+',pageSize: '+theSize+'}';
      let findSearch = '';
      if(this.findTxt != null && this.findTxt.trim().length > 0){
        findSearch = this.findSearch.replace("'%{0}%'",this.findTxt.trim())
      }
      var postData = {
        orgId: this.theOrg,
        menuKey: this.theData.key,
        dataType: this.dataType,
        selectType: this.selectType,
        filter: this.filter,
        findSearch: findSearch,
        searchData: searchData
      };
      console.log('post find:>>');
      console.log(postData);
      let postUrl = this.changeUrl(this.submitUrl);
      console.log('postUrl:>> '+postUrl);
      let { data: res } = await this.$http.post(postUrl, postData);
      if (res.result) {
        this.pageParam = {
          pageIndex: res.obj.pageIndex,
          pageSize: res.obj.pageSize,
          pageNum: res.obj.pageNum,
          pageCount: res.obj.pageCount
        };
        this.selectedIds = '';
        this.dataItems = res.obj.items;
      } else {
        this.clewMsg(res.type,res.msg);
      }
    },
    //分页切换分页数量
    handleSizeChange(val){
      this.pageParam.pageSize = val;
      let index = this.pageParam.pageIndex != null ? this.pageParam.pageIndex : 1;
      let size = this.pageParam.pageSize;
      this.findData(index,size);
    },
    //分页选择第几页
    handleCurrentChange(val){
      this.pageParam.pageIndex = val;
      let index = this.pageParam.pageIndex;
      let size = this.pageParam.pageSize != null ? this.pageParam.pageSize : 20;
      this.findData(index,size);
    }, 
    //勾选的时候来判断 让其变成单选
    handleSelect(val,row){
      if (val.length > 1) {
        if(this.selectType === 'select'){
          this.$refs.multipleTable.clearSelection() // 清空所有选择
          val.shift()
          this.$refs.multipleTable.toggleRowSelection(row) //  选中当前选择
        }else if(this.searchType === 'selectMany'){
          this.$refs.multipleTable.toggleRowSelection(row) //  选中当前选择
        }        
      }
      let selected = val.length && val.indexOf(row) !== -1
      //console.log(selected)  // true就是选中，0或者false是取消选中
    },
    //行选择
    handleSelectionChange(val){
      let posy = val;
      let pos = [];
      this.selectedIds = '';
      posy.forEach(item=>{
        pos.push(item.id);
        this.selectedIds = pos.join(',')//把选中的id已字符串传递出去  例如：'1,3,4,5'
      });
    },
    returnSelectData(){
      this.$parent.updateListData(this.selectedIds,this.returnUrl,this.returnType);
    },
    closeWin(){
      this.$parent.closeSubWin();
    },
    //处理URL自增加组织ID
    changeUrl(url){
      if(/\?|&/.test(url)){
        url += '&orgId=' + this.theOrg;
      }else{
        url += '?orgId=' + this.theOrg;
      }
      return url;
    },
    //提款信息
    clewMsg(type,mes){
      var theType = 'warning';
      if(type == 1)
        theType = 'success';
      else if(type == 3)
        theType = 'error';
      this.$message({
        dangerouslyUseHTMLString:true,
        message: mes,
        type: theType
      });
    }
  }
};
</script>
<style lang="less" scoped>
  .el-input{
    /deep/.el-input__inner{
      width: 260px;
      height: 34px;
      line-height: 34px;
      border: solid 1px #DDD;
      padding: 8px 10px;
      font-size: 15px;
      color: #222;
      border-radius: 4px;
    }
  }
  .table{
    clear: both;
    width: 100%;
    align-items: center;
    padding: 0;
    margin: 0;
    /deep/.el-table{
      width:100%;
      overflow: auto;
      max-height:calc(100% - 200px);
    }
    /deep/.el-table__header th{
      background: #EEE;
      font-weight:normal;
      color: #000;
    }
    /deep/.el-table__header .el-table__cell{
      padding: 5px 0;
    }
    /deep/.el-table__body-wrapper{
      width:100%;
    }
    /deep/tr .el-checkbox__input{
      margin-left: 10px;
    }
    /deep/td .el-checkbox__input{
      margin-left: 10px;
    }
    /deep/.el-checkbox__inner{
      width: 18px;
      height: 18px;
    }
    /deep/.el-checkbox__inner:hover{
      border-color: #00A578;
      box-shadow: 0 0 2px 1px rgba(42, 51, 49, 0.15);
    }
    /deep/.el-table thead{
      color: #FFF;
    }
    /deep/.el-table__empty-block{
      width: 0px;
    }
    /deep/.el-table__empty-text{
      text-align: left;
    }    
  }
  .paging {
    height: 40px;
    line-height: 30px;
    text-align: right;
    /deep/.el-pagination__total {
      line-height: 30px;
    }
    /deep/.el-pager {
      background: #FFFFFF;
      color:#00A578;
      border-radius: 4px;
      cursor: pointer !important;
      border: 1px solid #00A578;
      margin-right: 8px;
    }
    /deep/.el-pagination .btn-prev {
      background: #FFF;
      border: solid 1px #DDD;
      color: #999;
      border-radius: 4px;
      height: 32px;
      line-height: 30px;
      margin-right: 8px;
    }
    /deep/.el-pagination .btn-next {
      background: #FFF;
      border: solid 1px #DDD;
      color: #999;
      border-radius: 4px;
      height: 32px;
      line-height: 30px;
      margin-right: 3px;
    }
    /deep/.el-input__inner{
      border-radius: 4px;
      height: 32px;
      line-height: 30px;
    }
    /deep/.el-input__inner:hover{
      border: solid 1px #00A578;
      box-shadow: 0 0 3px 1px rgba(0,160,120,0.2);
    }
    /deep/.el-select-dropdown__item .selected{
      color: #000;
    }
    /deep/.number{
      color: #00A578;
    }
    /deep/.el-icon{
      font-size: 16px;
    }
  }
</style>
<style>
  .baseEdit{
    position: fixed;
    z-index: 555;
    top: 100px;
    left: calc((100% - 800px) / 2);
    width: 800px;
    max-height: 650px;
    overflow: hidden;
    background: #FFF;
    border-radius: 5px;
    box-shadow: 0 1px 2px 1px rgba(0,0,0,0.15);
  }
  .orderEdit{
    position: fixed;
    z-index: 666;
    top: 100px;
    left: calc((100% - 1300px) / 2);
    width: 1300px;
    max-height: 650px;
    overflow: hidden;
    background: #FFF;
    border-radius: 5px;
    box-shadow: 0 1px 2px 1px rgba(0,0,0,0.15);
  }
  .floatBG{
    position: fixed;
    z-index: 99;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.3);
  }
</style>
